package com.ax.center.ExceptionHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;


/**
 * @author zy
 * @date 2023/7/12 21:04
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private  final static Logger log= LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseEntity handleException(Exception e) {
        log.error("控制器层发生异常", e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
    }

    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public ResponseEntity handleBusinessException(BusinessException e) {
        log.error("业务异常", e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("业务异常：" + e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = SQLException.class)
    public ResponseEntity handleSQLException(SQLException e) {
        log.error("数据库异常", e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("数据库异常：" + e.getMessage());
    }


}
